#include <WiFi.h>
#include <WiFiMulti.h>
#include <WebServer.h>
#include <WiFiClient.h>

void wificonnect();

WiFiMulti wifimulti;
WiFiClient wifiClient;
const char *host = "192.168.4.1";//修改为Server服务端的IP，即你电脑的IP，确保在同一网络之下。
const int tcpPort = 8266;//修改为你建立的Server服务端的端口号，此端口号是创建服务器时指定的。


void setup()
{
    Serial.begin(115200);
    delay(3000);
    wificonnect();
    while (!wifiClient.connected()) //若未连接到服务端，则客户端进行连接。
    {
        if (!wifiClient.connect(host, tcpPort)) //实际上这一步就在连接服务端，如果连接上，该函数返回true
        {
            Serial.println("connection....");
            delay(500);
        }
    }
}

void loop()
{
    static int a=0;
    if (a%2 == 0)
    {
        wifiClient.print("qweA");
        Serial.println("Send A");
    }
    else
    {
        wifiClient.print("qwea");
        Serial.println("Send a");
    } 
    a++;
    delay(1000);
}

void wificonnect()
{
    //在这里添加你需要的WiFi账号密码
    // wifimulti.addAP("LAGRANGE", "abcdef..");
    // wifimulti.addAP("OpenWRT-2.4G", "skabcdef");
    // wifimulti.addAP("MEIZU 16s Pro", "skabcdef");
    // wifimulti.addAP("Feng", "1234567890");
    wifimulti.addAP("ESPap", "12345678");
    int i = 0;
    Serial.println("connecting.....");
    while (wifimulti.run() != WL_CONNECTED)
    {
        delay(1000);
        Serial.print(i);
        Serial.print(' ');
        i++;
        if (i > 20)
            break;
    }
    if (WiFi.status() != WL_CONNECTED)
        Serial.println("connect timeout");
    else
    {
        Serial.println("WiFi Connect");
        Serial.print("IP address:");
        Serial.println(WiFi.localIP());
    }
}